package com.j.thetabk.thetas;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by j on 2016/4/14.
 */
public class ThetaSConnectorD extends ThetaSConnectorC {

    public ThetaSPropertyB [] properties=null;

    public static void AddPropertyName(JSONArray jainout, int [] indexes)
    {
        for (int indexe : indexes) jainout.put(ThetaSProperty.propertyName[indexe]);
    }

    public static void AddPropertyNameSupport(JSONArray jainout, int [] indexes)
    {
        for (int indexe : indexes) jainout.put(ThetaSProperty.propertyName[indexe] + support);
    }

    public ThetaSConnectorD(String cameraIpAddress) {
        super(cameraIpAddress);
        properties=new ThetaSPropertyB[13];
        for(int i=0;i<13;i++)
            properties[i]=new ThetaSPropertyB();
    }

    protected boolean UpdateProperty(int []idx) throws IOException, JSONException {
        JSONArray jaProperties=new JSONArray();
        AddPropertyName(jaProperties, idx);
        AddPropertyNameSupport(jaProperties, idx);
        JSONObject input=AcquirePropertyParam(jaProperties, sessionId);
        JSONObject output = TransmitA(3, input);
        if(IsStateDone(output)) {
            JSONObject jb1=GetProperties(output);
            for (int anIdx : idx) {
                properties[anIdx].ParseChoiceAndSupport(anIdx, jb1);
            }
            return true;
        }
        return false;
    }

    public boolean UpdateProperties() throws IOException, JSONException {
        int []idx;
        if(FirmwareVersionNumber()>=1.42)
            idx= new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12};
        else
        {
            idx= new int[]{0,1,2,3,4,5,6,7,8,9,10,11};
            properties[12].support=ThetaSProperty.ExposureDelaySupport();
            properties[12].choice=0;
            properties[12].UpdateDisplayList(12);
        }
        boolean flg=UpdateProperty(idx);

        int []indices={0,1,2,3};
        for(int ai:indices)
            properties[ai].SyncSelection();

        return flg;
    }


    public boolean AcquireProperty(int []idx) throws IOException, JSONException {

        JSONArray jaProperties=new JSONArray();
        AddPropertyName(jaProperties, idx);
        JSONObject input=AcquirePropertyParam(jaProperties, sessionId);
        JSONObject output = TransmitA(3, input);
        if(IsStateDone(output)) {

            JSONObject jb1=GetProperties(output);

            for (int anIdx : idx) {
                properties[anIdx].ParseChoice(anIdx,jb1);
            }

            return true;
        }
        return false;
    }

    public int [] SetProperty(int index) throws JSONException, IOException {

        boolean notHasDelay=(FirmwareVersionNumber()<1.42);

        if(notHasDelay && index==12) {
            return new int[]{index};
        }

        JSONObject input=SetPropertyParam(properties[index].PropertyItem(index), sessionId);

        JSONObject output = TransmitA(3, input);

        if(IsStateDone(output))
        {
            switch(index)
            {
                case 4:
                {
                    if(UpdateProperty(new int[]{0,1,2,3,5}))
                        return new int[]{0,1,2,3,4,5};
                    return new int[]{4};
                }
                case 5:
                {
                    if(UpdateProperty(new int[]{0,1,2,3}))
                        return new int[]{0,1,2,3,5};
                    return new int[]{5};
                }
                case 6:
                {
                    if(notHasDelay) {
                        if (UpdateProperty(new int[]{0, 1, 2, 3, 4, 5, 10}))
                            return new int[]{-1, 0, 1, 2, 3, 4, 5, 6, 10};
                        return new int[]{6};
                    }
                    if (UpdateProperty(new int[]{0, 1, 2, 3, 4, 5, 10, 12}))
                        return new int[]{-1, 0, 1, 2, 3, 4, 5, 6, 10, 12};
                    return new int[]{6};
                }
                case 10:
                    if(GetValue())
                        return new int[]{-1,10};
                    return new int[]{10};
                default:
                    return new int[]{index};
            }
        }
        return null;
    }

    public int Mode()
    {
        return properties[6].choice;
    }

    public String StorageStr()
    {
        return super.StorageStr(Mode());
    }

    public int BracketNum()
    {
        int bn=0;
        for(int i=0;i<4;i++)
        {
            if(properties[i].checkedNum>0)
            {
                if(bn>0)
                    bn*=properties[i].checkedNum;
                else
                    bn=properties[i].checkedNum;
            }
        }
        return bn;
    }


    public void BracketList(ArrayList<Integer> settingIdx, ArrayList<Integer> propertyIdx)
    {
        ArrayList<Integer> wbl=new ArrayList<Integer>();
        int wbn=properties[0].BracketList(wbl);
        ArrayList<Integer> isol=new ArrayList<Integer>();
        int ison=properties[1].BracketList(isol);
        ArrayList<Integer> ssl=new ArrayList<Integer>();
        int ssn=properties[2].BracketList(ssl);
        ArrayList<Integer> evl=new ArrayList<Integer>();
        int evn=properties[3].BracketList(evl);

        boolean flg[]={false,false,false};

        if(ison==0)
        {
            //ev,ss,wb bracket
            int pptIndex[]={3,2,0};
            ThetaSPropertyA.AddList(settingIdx, propertyIdx, evl, ssl, wbl, pptIndex, flg);
            return;
        }
        if(ssn==0)
        {
            //ev, iso, wb bracket
            int pptIndex[]={3,1,0};
            ThetaSPropertyA.AddList(settingIdx, propertyIdx, evl, isol, wbl, pptIndex, flg);
            return;
        }
        //iso, ss, wb bracket
        int pptIndex[] = {2,1,0};
        ThetaSPropertyA.AddList(settingIdx, propertyIdx, ssl, isol, wbl, pptIndex, flg);
    }

    public void SelfTimerCountDown(int delay, ThetaSBKEventListener listener) throws InterruptedException {
        int recLen=delay;
        while(recLen>0)
        {
            listener.onCountDown(recLen);
            Thread.sleep(1000);
            recLen--;
        }
        listener.onCountDown(recLen);
    }


    public String TakePhotoWithDelay(ThetaSBKEventListener listener, int sleepms) throws IOException, JSONException, InterruptedException {
        JSONObject input;
        input=ShootStillImageParam(sessionId);
        JSONObject output = TransmitA(3, input);

        SelfTimerCountDown(properties[12].choice,listener);

        String commandId = output.getString("id");
        while (!IsStateDone(output)) {
            Log.v("TakePhoto", output.toString());
            Thread.sleep(sleepms);
            input=CheckStatusParam(commandId);
            output =TransmitA(4,input);
        }
        Log.v("TakePhoto",output.toString());
        GetState();
        GetValue();
        return GetFileUri(output);
    }

}
